package lumis.service.wsrp.consumer.doui;

import lumis.doui.control.Control;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.manager.ManagerFactory;
import lumis.service.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
import lumis.util.TextUtil;

import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;

/**
 * {@link Control} that renders an admin list if service instance is configured.
 * 
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
public class RemotePortletConfigurationInterfaceControl extends Control
{
	@Override
	public void buildSubControls() throws PortalException
	{
		super.buildSubControls();

		// gets caller service interface instance id to get its custom
		// properties
		String callerServiceInterfaceInstanceId = douiContext.getRequest().getCallerServiceInterfaceInstanceId();
		if (callerServiceInterfaceInstanceId == null)
			throw new PortalException("STR_NULL_CALLER_SERVICE_INTERFACE_INSTANCE_ID");

		// check if current service instance is configured
		String serviceInstanceId = douiContext.getRequest().getServiceInstanceConfig().getId();
		boolean configured;
		try
		{
			configured = !(ProducerEntityManagerFactory.getProducerEntityManager(serviceInstanceId).getProducerEntityIds().isEmpty());
		}
		catch (WSRPConsumerException e)
		{
			throw new UnexpectedException(e);
		}

		// if service instance is configured, we create an admin list
		StringBuilder controls = new StringBuilder(1500);
		controls.append("<control type=\"lum_generic\">\n");
		if (configured)
		{
			String currentValue = ManagerFactory.getServiceInterfaceInstanceManager().getCustomProperty(
					douiContext.getRequest().getSessionConfig(), douiContext.getRequest().getCallerServiceInterfaceInstanceId(),
					RemotePortletConfigurationInterfaceConstants.REMOTE_PORTLET_HANDLE_CUSTOM_PROPERTY_ID, douiContext.getTransaction());

			controls.append("	<control type=\"lum_propertyPage\" renderRequiredValuesDescriptor=\"false\" renderButtonsType=\"none\">\n");
			controls.append("		<control type=\"lum_interfaceHeaderButtons\" id=\"interfaceHeaderButtons\">\n");
			controls.append("			<control type=\"lum_refreshButton\">\n");
			controls.append("				<onClick type=\"processAction\" processActionId=\"refreshServiceDescription\" />\n");
			controls.append("			</control>\n");
			controls.append("		</control>\n");

			controls.append("		<control type=\"lum_adminList\" id=\"adminList\" dataId=\"portletHandle\" sourceId=\"remoteportletconfiguration\"");
			controls.append(" listHeight=\"380\">\n");
			controls.append("			<allowSelectRow type=\"singleSelect\"/>\n");
			controls.append("		</control>\n");
			controls.append("		<control type=\"lum_table\" width=\"100%\" cellspacing=\"0\">\n");
			controls.append("			<tr>\n");
			controls.append("				<td valign=\"top\">\n");
			controls.append("					<control type=\"lum_requiredValuesDescriptor\" />\n");
			controls.append("				</td>\n");
			controls.append("				<td>\n");
			controls.append("					<control type=\"lum_interfaceButtons\" id=\"interfaceButtons.autopopup\">\n");
			controls.append("						<control type=\"lum_okButton\" id=\"buttonOk\">\n");
			controls.append("							<onClick processActionId=\"commit\" type=\"processAction\">\n");
			controls.append("								<validators>\n");
			controls.append("									<validator controlId=\"adminList.tabulardata\" type=\"selectedOne\" />\n");
			controls.append("								</validators>\n");
			controls.append("							</onClick>\n");
			controls.append("						</control>");
			controls.append("						<control type=\"lum_cancelButton\" id=\"buttonCancel\">\n");
			controls.append("							<onClick type=\"closeWindow\" />\n");
			controls.append("						</control>\n");
			controls.append("					</control>\n");
			controls.append("				</td>\n");
			controls.append("			</tr>\n");
			controls.append("		</control>\n");
			controls.append("	</control>\n");

			if (currentValue != null)
			{
				controls.append("	<control type=\"lum_script\" disable-output-escaping=\"yes\">\n");
				controls.append("		<script><![CDATA[\n");
				controls.append("			var currentValue = '" + TextUtil.escapeJs(currentValue) + "';\n");
				controls.append("			var arrRadios = document.getElementsByName(\"adminList.tabulardata\");\n");
				controls.append("			for(var i = 0; i < arrRadios.length; i++)\n");
				controls.append("			{\n");
				controls.append("				if(currentValue == arrRadios[i].value)\n");
				controls.append("				{\n");
				controls.append("					arrRadios[i].checked = true;\n");
				controls.append("					break;\n");
				controls.append("				}\n");
				controls.append("			}\n");
				controls.append("		]]></script>\n");
				controls.append("	</control>\n");
			}
		}

		// else, we create an error message
		else
		{
			controls.append("	<control type=\"lum_propertyPage\" renderRequiredValuesDescriptor=\"false\" renderButtonsType=\"none\">\n");
			controls.append("		<control type=\"lum_controlGroup\" title=\"STR_BASIC_INFORMATION\">\n");
			controls.append("			<control type=\"lum_div\" style=\"height: 463px; overflow: auto;\">\n");
			controls.append("				<control type=\"lum_text\" text=\"" + localize("STR_CONSUMER_NOT_CONFIGURED") + "\" />\n");
			controls.append("			</control>\n");
			controls.append("		</control>\n");
			controls.append("		<control type=\"lum_table\" width=\"100%\" cellspacing=\"0px\" style=\"padding-top:10px\">\n");
			controls.append("			<tr>\n");
			controls.append("				<td>\n");
			controls.append("					<control type=\"lum_buttonList\" align=\"right\">\n");
			controls.append("						<control type=\"lum_cancelButton\" text=\"STR_CLOSE\" />\n");
			controls.append("					</control>\n");
			controls.append("				</td>\n");
			controls.append("			</tr>\n");
			controls.append("		</control>\n");
			controls.append("	</control>\n");
			
		}

		/*
		<control:lum_table width="100%" cellspacing="0">
			<tr>
				<td valign="top"><control:lum_requiredValuesDescriptor  /></td>
				<td>
					<control:lum_interfaceButtons id="interfaceButtons.standard">
						<control:lum_okButton />
						<control:lum_cancelButton>
							<onClick type="link">
								<hyperLink interfaceId="myBlogs"/>
							</onClick>
						</control:lum_cancelButton>
					</control:lum_interfaceButtons>
				</td>
			</tr>
		</control:lum_table>
		*/
		controls.append("</control>");
		appendSubControls(controls.toString());
	}
}
